﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using QLTB.Data;
using QLTB.Entities;
using _4rumDCT.App_Code;

namespace QuanLyThietBi.DCTEDU_.DanhMuc
{
    public partial class DMPhanMem : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Pager1.CurrentIndex = 1;
                Bind_Data(Pager1.CurrentIndex);
            }
        }

        protected void Bind_Data(int PageNo)
        {
            int TotalRows = 0;
            string WhereClause = "1=1";
            DataSet dsPhanMem = DataRepository.Provider.ExecuteDataSet("PhanMem_GetPaged", WhereClause, "MaPhanMem DESC", PageNo - 1, Pager1.PageSize);
            gridCatalog.DataSource = dsPhanMem.Tables[0];
            gridCatalog.DataBind();
            try
            {
                TotalRows = Convert.ToInt32(dsPhanMem.Tables[1].Rows[0][0]);
            }
            catch (Exception)
            {
            }
            Pager1.ItemCount = TotalRows;
            if (TotalRows <= Pager1.PageSize)
                Pager1.Visible = false;
            else
                Pager1.Visible = true;
        }

        protected void Pager1_Command(object sender, CommandEventArgs e)
        {
            try
            {
                int currnetPageIndex = Convert.ToInt32(e.CommandArgument);
                Pager1.CurrentIndex = currnetPageIndex;
                Bind_Data(Pager1.CurrentIndex);
            }
            catch (Exception ex)
            {
                Common.WriteLog("DMPhanMem.aspx.cs", "Pager1_Command()", ex.ToString());
            }
        }

        protected void cmdSave_Click(object sender, EventArgs e)
        {
            bool updateStatus = false;
            try
            {
                PhanMem entPhanMem = new PhanMem();
                entPhanMem.MaPhanMem = Convert.ToInt32(H_CatalogId.Value);
                entPhanMem.TenPhanMem = txtTen.Text;
                entPhanMem.DungLuong = txtDungLuong.Text;
                entPhanMem.DuongDan = txtDuongDan.Text;
                entPhanMem.GhiChu = txtGhiChu.Text;
                if (H_CatalogId.Value == "0")
                {
                    updateStatus = DataRepository.PhanMemProvider.Insert(entPhanMem);
                }
                else
                {
                    entPhanMem.EntityState = EntityState.Changed;
                    updateStatus = DataRepository.PhanMemProvider.Update(entPhanMem);
                }
                if (updateStatus)
                {
                    ScriptManager.RegisterStartupScript(upPanel1, this.GetType(), Constant.THANHCONG, Constant.CAPNHAT_THANHCONG, true);
                    cmdReset_Click(sender, e);
                }
                else
                    ScriptManager.RegisterStartupScript(upPanel1, this.GetType(), Constant.THATBAI, Constant.CAPNHAT_THATBAI, true);
            }
            catch (Exception ex)
            {
                Common.WriteLog("DMPhanMem.aspx.cs", "cmdSave_Click", ex.ToString());
                ScriptManager.RegisterStartupScript(upPanel1, this.GetType(), Constant.THATBAI, Constant.CAPNHAT_THATBAI, true);
            }
        }

        protected void cmdReset_Click(object sender, EventArgs e)
        {
            Bind_Data(Pager1.CurrentIndex);
            txtTen.Text = "";
            txtDungLuong.Text = "";
            txtDuongDan.Text = "";
            H_CatalogId.Value = "0";
            txtGhiChu.Text = "";

        }

        protected void gridCatalog_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                ImageButton l = (ImageButton)e.Row.FindControl("cmdDelete");
                l.Attributes.Add("onclick", "javascript:return " + "confirm('Bạn chắc chắn muốn xóa thông tin này?');");

                e.Row.Attributes["onmouseover"] = "this.style.cursor='hand';this.style.backgroundColor='WhiteSmoke';";
                e.Row.Attributes["onmouseout"] = "this.style.backgroundColor='White';";
            }
        }

        protected void gridCatalog_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Select")
            {
                try
                {
                    H_CatalogId.Value = e.CommandArgument.ToString();
                    PhanMem entPhanMem = DataRepository.PhanMemProvider.GetByMaPhanMem(Convert.ToInt32(H_CatalogId.Value));
                    if (entPhanMem != null)
                    {
                        txtTen.Text = entPhanMem.TenPhanMem;
                        txtGhiChu.Text = entPhanMem.GhiChu;
                        txtDuongDan.Text = entPhanMem.DuongDan;
                        txtDungLuong.Text = entPhanMem.DungLuong;
                        Bind_Data(Pager1.CurrentIndex);
                    }
                }
                catch (Exception ex)
                {
                    Common.WriteLog("DMPhanMem.aspx.cs", "gridCatalog_RowCommand - Select", ex.ToString());
                }
            }
            if (e.CommandName == "Delete")
            {
                try
                {
                    string IdPhanMem = e.CommandArgument.ToString();
                    bool deleteStatus = DataRepository.PhanMemProvider.Delete(Convert.ToInt32(IdPhanMem));
                    if (!deleteStatus)
                        ScriptManager.RegisterStartupScript(upPanel1, this.GetType(), Constant.THATBAI, Constant.XOA_THATBAI, true);
                    else
                        ScriptManager.RegisterStartupScript(upPanel1, this.GetType(), Constant.THANHCONG, Constant.XOA_THANHCONG, true);
                    Bind_Data(Pager1.CurrentIndex);
                }
                catch (Exception ex)
                {
                    Common.WriteLog("DMPhanMem.aspx.cs", "gridCatalog_RowCommand - Delete", ex.ToString());
                    ScriptManager.RegisterStartupScript(upPanel1, this.GetType(), Constant.THATBAI, Constant.XOA_THATBAI, true);
                }
            }
        }

        protected void gridCatalog_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            cmdReset_Click(sender, e);
        }

    }
}